package com.tlgen.modules.common.controller;

import com.tlgen.common.exception.BadRequestException;
import com.tlgen.modules.common.domain.J_Area;
import com.tlgen.modules.common.service.J_AreaService;
import com.tlgen.modules.common.service.dto.J_AreaDto;
import com.tlgen.modules.common.service.dto.J_AreaQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Api(tags = "系统：乡镇村")
@RestController
@RequestMapping("/api/j_areas")
@SuppressWarnings("unchecked")
public class J_AreaController {

    private final J_AreaService j_AreaService;

    private static final String ENTITY_NAME = "j_area";

    public J_AreaController(J_AreaService j_AreaService) {
        this.j_AreaService = j_AreaService;
    }

    @ApiOperation("返回全部的tree,下拉使用")
    @GetMapping(value = "/tree")
    @PreAuthorize("@tlgen.check('j_area:list')")
    public ResponseEntity<Object> getJ_AreaTree(){
        return new ResponseEntity<>(j_AreaService.getJ_AreaTree(j_AreaService.findByPid(0L)), HttpStatus.OK);
    }


    @ApiOperation("查询列表")
    @GetMapping
    @PreAuthorize("@tlgen.check('j_area:list')")
    public ResponseEntity<Object> getMenus(J_AreaQueryCriteria criteria){
        List<J_AreaDto> jAreaDtoList = j_AreaService.queryAll(criteria);
        return new ResponseEntity<>(j_AreaService.buildTree(jAreaDtoList), HttpStatus.OK);
    }

    @ApiOperation("新增")
    @PostMapping
    @PreAuthorize("@tlgen.check('j_area:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody J_Area resources){
        if (resources.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        return new ResponseEntity<>(j_AreaService.create(resources), HttpStatus.CREATED);
    }


    @ApiOperation("修改")
    @PutMapping
    @PreAuthorize("@tlgen.check('j_area:edit')")
    public ResponseEntity<Object> update(@Validated(J_Area.Update.class) @RequestBody J_Area resources){
        j_AreaService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ApiOperation("删除")
    @DeleteMapping
    @PreAuthorize("@tlgen.check('j_area:del')")
    public ResponseEntity<Object> delete(@RequestBody Set<Long> ids){
        Set<J_Area> j_areaSet = new HashSet<>();
        for (Long id : ids) {
            List<J_Area> j_areaList = j_AreaService.findByPid(id);
            j_areaSet.add(j_AreaService.findOne(id));
            j_areaSet = j_AreaService.getDeleteJ_Areas(j_areaList, j_areaSet);
        }
        j_AreaService.delete(j_areaSet);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@tlgen.check('j_area:list')")
    public void download(HttpServletResponse response, J_AreaQueryCriteria criteria) throws IOException {
        j_AreaService.download(j_AreaService.queryAll(criteria), response);
    }

}
